home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / GETOPT1.C < prev    next >
C/C++ Source or Header  |  1990-04-16  |  4KB  |  153 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "getopt.h"
  19.  
  20. #ifdef __STDC__
  21. #define CONST const
  22. #else
  23. #define CONST
  24. #endif
  25.  
  26. int
  27. getopt_long (argc, argv, options, long_options, opt_index)
  28.      int argc;
  29.      char **argv;
  30.      CONST char *options;
  31.      CONST struct option *long_options;
  32.      int *opt_index;
  33. {
  34.   int val;
  35.  
  36.   _getopt_long_options = long_options;
  37.   val = getopt (argc, argv, options);
  38.   if (val == 0)
  39.     *opt_index = option_index;
  40.   return val;
  41. }
  42.  
  43. /* Like getopt_long, but '-' as well as '+' can indicate a long option.
  44.    If an option that starts with '-' doesn't match a long option,
  45.    but does match a short option, it is parsed as a short option
  46.    instead. */
  47.  
  48. int getopt_long_only (argc, argv, options, long_options, opt_index)
  49.      int argc;
  50.      char **argv;
  51.      CONST char *options;
  52.      CONST struct option *long_options;
  53.      int *opt_index;
  54. {
  55.   int val;
  56.  
  57.   _getopt_long_options = long_options;
  58.   _getopt_long_only = 1;
  59.   val = getopt (argc, argv, options);
  60.   if (val == 0)
  61.     *opt_index = option_index;
  62.   return val;
  63. }
  64.      
  65.  
  66. #ifdef TEST
  67.  
  68. #include <stdio.h>
  69.  
  70. int
  71. main (argc, argv)
  72.      int argc;
  73.      char **argv;
  74. {
  75.   char c;
  76.   int digit_optind = 0;
  77.  
  78.   while (1)
  79.     {
  80.       int this_option_optind = optind;
  81.       char *name = '\0';
  82.       int option_index = 0;
  83.       static struct option long_options[]
  84.     = {{ "add", 1, 0, 0 },
  85.        { "append", 0, 0, 0 },
  86.        { "delete", 1, 0, 0 },
  87.        { "verbose", 0, 0, 0 },
  88.        { "create", 0, 0, 0 },
  89.        { "file", 1, 0, 0 },
  90.        { 0, 0, 0, 0}};
  91.  
  92.       c = getopt_long (argc, argv, "abc:d:0123456789",
  93.                long_options, &option_index);
  94.       if (c == EOF)
  95.     break;
  96.     switch (c)
  97.       {
  98.       case 0:
  99.         printf ("option %s", (long_options[option_index]).name);
  100.         if (optarg)
  101.           printf (" with arg %s", optarg);
  102.         printf ("\n");
  103.         break;
  104.  
  105.       case '0':
  106.       case '1':
  107.       case '2':
  108.       case '3':
  109.       case '4':
  110.       case '5':
  111.       case '6':
  112.       case '7':
  113.       case '8':
  114.       case '9':
  115.         if (digit_optind != 0 && digit_optind != this_option_optind)
  116.           printf ("digits occur in two different argv-elements.\n");
  117.         digit_optind = this_option_optind;
  118.         printf ("option %c\n", c);
  119.         break;
  120.  
  121.       case 'a':
  122.         printf ("option a\n");
  123.         break;
  124.  
  125.       case 'b':
  126.         printf ("option b\n");
  127.         break;
  128.  
  129.       case 'c':
  130.         printf ("option c with value `%s'\n", optarg);
  131.         break;
  132.  
  133.       case '?':
  134.         break;
  135.  
  136.       default:
  137.         printf ("?? getopt returned character code 0%o ??\n", c);
  138.       }
  139.     }
  140.  
  141.   if (optind < argc)
  142.     {
  143.       printf ("non-option ARGV-elements: ");
  144.       while (optind < argc)
  145.     printf ("%s ", argv[optind++]);
  146.       printf ("\n");
  147.     }
  148.  
  149.   return 0;
  150. }
  151.  
  152. #endif /* TEST */
  153.